#ifndef FILE_SYSTEM_H
#define FILE_SYSTEM_H

#include "../../include/gudb.h"
#include <stdint.h>
#include <stdbool.h>

// File types
#define FILE_TYPE_DATA   0
#define FILE_TYPE_WAL    1
#define FILE_TYPE_BINLOG 2
#define FILE_TYPE_META   3
#define FILE_TYPE_LOG    4

// Page operations result codes
#define PAGE_OK           0
#define PAGE_ERR_NOMEM    1
#define PAGE_ERR_IO       2
#define PAGE_ERR_CORRUPT  3
#define PAGE_ERR_FULL     4

// LSN (Log Sequence Number) management
typedef struct {
    uint64_t current_lsn;
    uint64_t checkpoint_lsn;
    uint64_t flush_lsn;
} lsn_manager;

// File descriptor management
typedef struct {
    int fd;
    char filepath[256];
    uint64_t size;
    bool is_mmaped;
    void* mmap_addr;
    size_t mmap_size;
} file_handle;

// Page buffer for I/O operations
typedef struct {
    gudb_page* page;
    uint32_t page_id;
    bool dirty;
    bool valid;
    uint64_t last_access;
} page_buffer;

// File system context
typedef struct {
    file_handle data_file;
    file_handle wal_file;
    file_handle binlog_file;
    file_handle meta_file;
    file_handle log_file;
    file_handle lock_file;
    
    lsn_manager lsn_mgr;
    page_buffer* page_buffers;
    uint32_t num_page_buffers;
    
    // Statistics
    uint64_t pages_read;
    uint64_t pages_written;
    uint64_t page_cache_hits;
    uint64_t page_cache_misses;
} file_system;

// File system initialization and cleanup
file_system* fs_init(const char* data_dir);
int fs_cleanup(file_system* fs);

// File operations
int fs_open_file(file_handle* handle, const char* filepath, int flags);
int fs_close_file(file_handle* handle);
int fs_sync_file(file_handle* handle);
int fs_truncate_file(file_handle* handle, uint64_t size);

// Page operations
int fs_read_page(file_system* fs, uint32_t page_id, gudb_page* page);
int fs_write_page(file_system* fs, uint32_t page_id, const gudb_page* page);
int fs_allocate_page(file_system* fs, uint32_t* page_id);
int fs_free_page(file_system* fs, uint32_t page_id);

// Page validation and checksums
uint32_t fs_calculate_checksum(const gudb_page* page);
bool fs_validate_page(const gudb_page* page);
int fs_repair_page(gudb_page* page);

// LSN management
uint64_t fs_get_next_lsn(file_system* fs);
int fs_set_page_lsn(gudb_page* page, uint64_t lsn);
uint64_t fs_get_page_lsn(const gudb_page* page);

// File locking
int fs_lock_database(file_system* fs);
int fs_unlock_database(file_system* fs);

// Memory mapping
int fs_mmap_file(file_handle* handle, size_t size);
int fs_munmap_file(file_handle* handle);

// Utility functions
uint64_t fs_get_file_size(const file_handle* handle);
bool fs_file_exists(const char* filepath);
int fs_create_directory(const char* dir_path);

// Debug and statistics
void fs_print_stats(const file_system* fs);
int fs_verify_integrity(file_system* fs);

// Accessor functions for file descriptors
int fs_get_data_fd(const file_system* fs);
int fs_get_wal_fd(const file_system* fs);
int fs_get_binlog_fd(const file_system* fs);
int fs_get_meta_fd(const file_system* fs);
int fs_get_log_fd(const file_system* fs);
int fs_get_lock_fd(const file_system* fs);

#endif // FILE_SYSTEM_H